﻿var grid, cbProgress;

//初始化表格
function InitGrid() {
    var SLOCATIONCODE = $.trim($("#txtSLOCATIONCODE").attr("value"));
    var SGOODSCODE = $.trim($("#txtSGOODSCODE").attr("value"));
    var INVGOODSCODE = $.trim($("#txtINVGOODSCODE").attr("value"));
    var SGOODSNAME = $.trim($("#txtSGOODSNAME").attr("value"));
    var INVGOODSNAME = $.trim($("#txtINVGOODSNAME").attr("value"));
    grid = $('#tab_list').datagrid({
        name: 'tbl_data',
        url: '/Location/QueryLocationGoods',
        title: '货位商品关联列表',
        queryParams: {
            SLOCATIONCODE: SLOCATIONCODE,
            SGOODSCODE: SGOODSCODE,
            INVGOODSCODE: INVGOODSCODE,
            SGOODSNAME: SGOODSNAME,
            INVGOODSNAME: INVGOODSNAME
        },
        //width: 815,
        height: 530,
        //border : false,
        //fit: false,
        fitColumns: false,
        //nowrap: true,
        rownumbers: true,
        //showFooter: true,
        //loadMsg: '正在加载信息...',
        //frozen: true,
        //sortName: "CODE", //排序设置
        singleSelect: true,
        idField: 'LOCATIONGOODSID',
        columns: [[
        //{ field: 'ck', checkbox: true },
                    { field: 'LOCATIONGOODSID', title: '货位商品关联ID', hidden: true },
                    { field: 'LOCATIONID', title: '货位ID', hidden: true },
                    { field: 'GOODSID', title: '商品ID', hidden: true },
                    { field: 'PROCESSGOODSID', title: '加工商品ID', hidden: true },
                    { field: 'DIRECTSELLINGGOODSID', title: '下架商品ID', hidden: true },
                    { field: 'WOCGOODSID', title: 'WOC商品ID', hidden: true },
                    { field: 'OTHERGOODSID', title: '供应商商品ID', hidden: true },
                    { field: 'LOCATIONCODE', title: '货位编码', align: 'center', sortable: true },
                    { field: 'GOODSCODE', title: '上架商品编码', align: 'center', sortable: true },
                    { field: 'GOODSNAME', title: '上架商品名称', align: 'center', sortable: true },
                    { field: 'WAREHOUSENAME', title: '仓库', align: 'center', sortable: true },
                    { field: 'CELLNO', title: '库位', align: 'center', sortable: true },
                    { field: 'DIRECTSELLINGGOODSCODE', title: '下架商品编码', align: 'center', sortable: true },
                    { field: 'DIRECTSELLINGGOODSNAME', title: '下架商品名称', align: 'center', sortable: true },
                    //{ field: 'CREATER', title: '创建人', align: 'center', sortable: true },
                    //{ field: 'CREATEDATE', title: '创建时间', align: 'center', sortable: true, formatter: ChangeDateFormat },
                    //{ field: 'EDITOR', title: '修改人', align: 'center', sortable: true },
                    //{ field: 'EDITDATE', title: '修改时间', align: 'center', sortable: true, formatter: ChangeDateFormat },
                    { field: 'REMARK', title: '备注', align: 'center', sortable: true },
                    { field: 'VALID', title: '是否启用', align: 'center', sortable: true, formatter: formatvalid }
        ]],
        toolbar: [{
            id: 'btnAdd',
            text: '新增',
            iconCls: 'icon-add',
            handler: function () {
                add();
            }
        }, '-', {
            id: 'btnEdit',
            text: '修改',
            iconCls: 'icon-edit',
            handler: function () {
                edit();
            }
        }, '-', {
            id: 'btnRemove',
            text: '删除',
            iconCls: 'icon-remove',
            handler: function () {
                del();
            }
        }],
        pagination: true,
        pageSize: 20,
        pageNumber: 1,
        pageList: [10, 20, 30, 40, 50],
        rownumbers: true, //行号
        onLoadSuccess: function (data) {
            onRefreshButton();
        },
        onSelect: function (rowIndex, rowData) {
            onRefreshButton();
        },
        onUnselect: function (rowIndex, rowData) {
            onRefreshButton();
        },
        onSortColumn: function (sort, order) {
            grid.datagrid('reload');
        }
    });
    //清空选择
    grid.datagrid('clearSelections');
}

function formatvalid(val) {
    if (val != null) {
        if (val == 0) {
            return "启用";
        }
        if (val == 1) {
            return "不启用";
        }
    }
}

function ChangeDateFormat(val) {
    if (val != null) {
        var date = new Date(parseInt(val.replace("/Date(", "").replace(")/", ""), 10));
        //月份为0-11，所以+1，月份小于10时补个0
        var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
        var currentDate = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
        return date.getFullYear() + "-" + month + "-" + currentDate;
    }

    return "";
}

//控制按钮
function onRefreshButton() {
    $('#btnAdd').linkbutton('enable');
    $('#btnEdit').linkbutton('disable');
    $('#btnRemove').linkbutton('disable');

    //$('#btnImport').linkbutton('disable');
    // $('#btnExport').linkbutton('disable');

    var rows = grid.datagrid('getSelections');
    if (rows.length == 1) {
        $('#btnEdit').linkbutton('enable');
        $('#btnRemove').linkbutton('enable');
    }
}


//增加
function add() {
    $('#p1').dialog('open').dialog('setTitle', '新增货位商品关联信息');
    $("#hiddenLOCATIONGOODSID").val("");
    //$("#txtLocationCode").val("");
    $("#txtGoodsCode").val("");
    $("#txtDirectSellingGoodsCode").val("");
    $("#txtRemark").val("");
    $('#LocationCode').combogrid("setValue", "");
    //$('#GoodsCode').combogrid("setValue", "");
    $('#ProcessGoodsCode').combogrid("setValue", "0");
    //$('#DirectSellingGoodsCode').combogrid("setValue", "0");
    $('#WOCGoodsCode').combogrid("setValue", "0");
    $('#OtherGoodsCode').combogrid("setValue", "0");
    $('#Valid').combobox("setValue", "");
    url = '/Location/SaveLocationGoods';
}

//修改
function edit() {
    var rows = grid.datagrid('getSelections');
    $('#p1').dialog('open').dialog('setTitle', '修改货位商品关联信息');
    $("#hiddenLOCATIONGOODSID").val(rows[0].LOCATIONGOODSID);
    //$("#txtLocationCode").val(rows[0].LOCATIONCODE);
    $("#txtGoodsCode").val(rows[0].GOODSCODE);
    $("#txtDirectSellingGoodsCode").val(rows[0].DIRECTSELLINGGOODSCODE);
    $("#txtRemark").val(rows[0].REMARK);
    var LOCATIONID = rows[0].LOCATIONID;
    var GOODSID = rows[0].GOODSID;
    $('#LocationCode').combogrid("setValue", LOCATIONID);
    //$('#GoodsCode').combogrid("setValue", GOODSID);
    $('#ProcessGoodsCode').combogrid("setValue", rows[0].PROCESSGOODSID);
    //$('#DirectSellingGoodsCode').combogrid("setValue", rows[0].DIRECTSELLINGGOODSID);
    $('#WOCGoodsCode').combogrid("setValue", rows[0].WOCGOODSID);
    $('#OtherGoodsCode').combogrid("setValue", rows[0].OTHERGOODSID);
    $('#Valid').combobox("setValue", rows[0].VALID);
    url = '/Location/SaveLocationGoods';

}



//删除
function del() {
    var ids = [];
    var rows = grid.datagrid('getSelections');
    for (var i = 0; i < rows.length; i++) {
        ids.push(rows[i].LOCATIONGOODSID);
    }
    $.messager.confirm('提示信息', '您确认要删除吗?', function (data) {
        if (data) {
            $('#location').form('submit', {
                url: '/Location/DeleteLocationGoods' + '?ids=' + ids.join(','),
                success: function (data) {
                    eval('data=' + data);
                    if (data.success) {
                        InitGrid();
                    } else {
                        $.messager.alert('错误', data.msg, 'error');
                    }
                }
            })

        }
    });
}

function save() {
    var LOCATIONGOODSID = $.trim($("#hiddenLOCATIONGOODSID").attr("value"));
    //var LocationCode = $.trim($("#txtLocationCode").attr("value"));
    var GoodsCode = $.trim($("#txtGoodsCode").attr("value"));
    var LocationCode = $('#LocationCode').combobox('getValue');
    //var GoodsCode = $('#GoodsCode').combobox('getValue');
    //var GoodsCode_Text = $('#GoodsCode').combobox('getText');

    if (LocationCode == "") {
        $.messager.alert('提示', '货位不能为空', 'warning');
        $("#LocationCode").focus();
        return false;
    }
    if (GoodsCode == "") {
        $.messager.alert('提示', '商品不能为空', 'warning');
        $("#GoodsCode").focus();
        return false;
    }

    $('#location').form('submit', {
        url: url + '?LOCATIONGOODSID=' + LOCATIONGOODSID,
        success: function (data) {
            eval('data=' + data);
            if (data.success) {
                $('#p1').dialog('close');
                InitGrid();
            } else {
                $.messager.alert('错误', data.msg, 'error');
                //alert("保存失败！请检查");
            }
        }
    });
}


//提交参数
function submitParams() {
    $('#fm2').form('submit', {
        url: '/Distr/UpdateParams',
        success: function (data) {
            eval('data=' + data);
            if (data.success) {
                grid.datagrid('reload');
                $.messager.alert('提示', '参数修改成功!', 'info');
            } else {
                $.messager.alert('错误', data.msg, 'error');
            }
        }
    });
}

